package com.sercomm.ott.mediaplayer.viewpager_test;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.sercomm.ott.mediaplayer.R;
import com.sercomm.ott.mediaplayer.viewpager_test.adapter.*;
import com.sercomm.ott.mediaplayer.viewpager_test.view.*;



/**
 * Created by davis on 17-10-30.
 */

public class ViewPagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    public void startViewActivity(Activity activity,int tab){
        Intent intent = new Intent(activity,ViewPagerActivity.class);
        intent.putExtra("tab",tab);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | intent.FLAG_ACTIVITY_SINGLE_TOP);
        activity.startActivity(intent);
    }
    /**
     * tab图标集合
     */
    private final int ICONS_RES[][] = {
            {
                    R.mipmap.ic_home_normal,
                    R.mipmap.ic_home_focus
            },
            {
                    R.mipmap.ic_message_normal,
                    R.mipmap.ic_message_focus
            },

            {
                    R.mipmap.ic_mine_normal,
                    R.mipmap.ic_mine_focus
            }
    };
    /** tab 颜色值*/
    private final int[] TAB_COLORS = new int []{
            R.color.main_bottom_tab_textcolor_normal,
            R.color.main_bottom_tab_textcolor_selected};

    private Fragment[] fragments = {
            new HomeFragment(),
            new MessageFragment(),
            new MineFragment()
    };

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_main);
        initViews();
    }
    private void initViews(){
        TabFragmentAdapter mAdapter =new TabFragmentAdapter(getSupportFragmentManager(),fragments);
        ViewPager mPager = (ViewPager)findViewById(R.id.tab_pager);
        mPager.setOffscreenPageLimit(1);
        mPager.setAdapter(mAdapter);

        TabContainerView mTabLayout = (TabContainerView)findViewById(R.id.ll_tab_container);
        mTabLayout.setOnPageChangeListener(this);
        mTabLayout.initContainer(getResources().getStringArray(R.array.tab_main_title), ICONS_RES, TAB_COLORS, true);

        int width = getResources().getDimensionPixelSize(R.dimen.tab_icon_width);
        int height = getResources().getDimensionPixelSize(R.dimen.tab_icon_height);
        mTabLayout.setContainerLayout(R.layout.viewpager_tab_containter_view,R.id.iv_tab_icon, R.id.tv_tab_text, width, height);

        mTabLayout.setViewPager(mPager);
        mPager.setCurrentItem(getIntent().getIntExtra("tab",0));

    }
    @Override
    public void onPageScrolled(int position,float positionOffset,int positionOffsetPixel){

    }
    @Override
    public void onPageSelected(int posttion){

    }
    @Override
    public void onPageScrollStateChanged(int state){

    }
    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
    }
}
